﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Configuration;

namespace Dracham.Classes
{
    public enum AccessLevels : int
    {
        PlayerLevel =       1,
        GameMaster =        2,
        Administrator =     3
    }
    
    public partial class Account : Object
    {
        public DrachamNetwork Client { get; set; }
        public Character CurrentCharacter { get; set; }
        public String SessionKey { get; set; }

        public static Account ValidateLogin(string username, string password)
        {
            String hashedPassword = Utility.GetMd5Hash(password);
            return GameWorld.Accounts.Single(a => a.Username == username && a.Password == hashedPassword);
        }

        public static void HandleLogin(DrachamNetwork client)
        {
            String username = client.ReceiveString(50);
            String password = client.ReceiveString(50);

            if (!String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(password))
            {
                Account acc = Account.ValidateLogin(username, password);

                if (acc != null)
                {
                    String sessionKey = Guid.NewGuid().ToString();

                    acc.CurrentCharacter = acc.Characters[0];
                    acc.Client = client;
                    acc.SessionKey = sessionKey;

                    #region Set up Handlers

                    client.AddHandler(MessageType.ChatMessage, MessageHandler.Instance.HandleMessage);
                    client.AddHandler(MessageType.CharacterMovement, Character.CharacterMovement);
                    client.AddHandler(MessageType.AttackRequest, FightHandler.Instance.Handle);

                    #endregion

                    client.Invoke(MessageType.LoginSuccess, 
                        new NetworkParameter(sessionKey, 36), new NetworkParameter(DateTime.Now.Ticks.ToString(), 19));

                    client.Invoke(MessageType.FullCharacterData, acc.CurrentCharacter.CharacterDescription());

                    List<NetworkParameter> netParams = new List<NetworkParameter>();
                    netParams.Add(new NetworkParameter(acc.CurrentCharacter.Position.ZoneId, 20));
                    netParams.AddRange(acc.CurrentCharacter.Position.GetNetworkDescription());
                    client.Invoke(MessageType.CharacterMovement, netParams.ToArray());

                    foreach (Account account in acc.CurrentCharacter.Position.Zone.GetCharactersInZone().Where(a => a != acc))
                    {
                        netParams.Clear();
                        netParams.Add(new NetworkParameter(acc.CurrentCharacter.Id.ToString(), 36));
                        netParams.Add(new NetworkParameter(acc.CurrentCharacter.ModelId.ToString(), 20));
                        netParams.Add(new NetworkParameter(acc.CurrentCharacter.Position.ZoneId, 20));
                        netParams.AddRange(acc.CurrentCharacter.Position.GetNetworkDescription());
                        netParams.AddRange(acc.CurrentCharacter.Position.GetNetworkDescription());
                        account.Client.Invoke(MessageType.MobMovement, netParams.ToArray());
                    }

                    acc.CurrentCharacter.Position.Zone.PlayerEnterZone();

                    return;
                }
            }

            client.Invoke(MessageType.LoginFailure);
        }
    }
}
